﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System;
using System.Data;
using System.Data.Common;
using EntLibContrib.Query.Commands;

namespace EntLibContrib.Query.QuickStart.CustomCommands
{
    using Configuration;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel;

    /// <summary>
	/// Custom data command to read a set of active Basic1 objects sorted in SampleString order
	/// </summary>
    [ConfigurationElementType(typeof(ReadBasic1ObjectSortCommandData))]
    public class ReadBasic1ObjectSortCommand : DataCommand
	{
		#region Constants
		private const string commandText = "select b.* from [Query].[Domain].[Basic1] as b inner join [QueryAudit].[Standard].[Audit] as a on b.Id = a.PrimaryId where (a.DeletedDate is null) order by SampleString";
		#endregion

		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="ReadBasic1ObjectSortCommand"/> class.
		/// </summary>
		public ReadBasic1ObjectSortCommand()
		{
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Executes a read action.
		/// </summary>
		/// <returns>
		/// a DataSet containing the results of the read action
		/// </returns>
		public override DataSet ExecuteForRead()
		{
			// read the xml document
			DbCommand dbCommand = Database.GetSqlStringCommand(commandText);
			//return the dataset
			return Database.ExecuteDataSet(dbCommand);
		}

		/// <summary>
		/// Executes a write action.
		/// </summary>
		public override void ExecuteForWrite()
		{
			throw new NotImplementedException();
		}
		#endregion
	}

    public class ReadBasic1ObjectSortCommandData : CommandData
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ReadBasic1ObjectSortCommandData"/> class.
        /// </summary>
        public ReadBasic1ObjectSortCommandData()
            : base(typeof(ReadBasic1ObjectSortCommand))
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ReadBasic1ObjectSortCommandData"/> class.
        /// </summary>
        public ReadBasic1ObjectSortCommandData(string name)
            : base(name, typeof(ReadBasic1ObjectSortCommand))
        {
        }

        public override Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.TypeRegistration GetRegistration()
        {
            return
                new TypeRegistration<ICommand>(
                    () => new ReadBasic1ObjectSortCommand())
                {
                    Lifetime = TypeRegistrationLifetime.Transient,
                    Name = Name,
                    IsPublicName = true
                };
        }
    }
}
